
import 'dart:convert';

import 'package:flutter_easyloading/flutter_easyloading.dart';

import 'tool_show_loading.dart';

import 'tool_http_request.dart';

typedef errorCallBack<T> = void Function(T error);
typedef dataCallBack<T> = void Function(T success);

class ApiUrl {

  static ShowLoadingViewDialog _loadingViewDialog = ShowLoadingViewDialog();

  static bool _isShow = true;

  static var _dict = {
    'tantou':'https://tianpuchat.bjmantis.net/chat/t1/chat.html?mcid=6149&mpid=5f5720b3f61b14393ea97375',
    'homeBannerUrl':'https://api.joheeyoo.com/mobile/get_focus_map',
    'placeHolderImg':'emtpy_class_412',
    'placeHolderTeacherImg':'big_teacher_412',
    'registProtocol':'https://registration.joheeyoo.com',
    'privacyProtocol':'https://privacy.joheeyoo.com',
    'checkUpdataUrl':'https://api.joheeyoo.com/mobile/version',
    'iapServiceName':'IAPHelper412',
    'homeNewsUrl':'http://info.topeduol.com.cn/cmsInformation',
    'base' : 'https://tianpuappschool.bjmantis.cn/hc-app',
  };

  ///获取专业
  static Future<void> getHomeProductInfo(context, dataCallBack success, errorCallBack error) async {
    return _httpCustom(context,'/home/productInfo',{
      'companyId': '6149',
      'serviceAgentId' : '412'
    },success,error);
  }
  ///获取系统专业课程
  static Future<void> getHomeQueryCourse(context, String key, dataCallBack success, errorCallBack error) async {
    var param = {
      'pageNum' : '1',
      'pageSize' : '100',
      'condition' : {
        'secondProductId' : key,
        'companyId': '6149',
        'serviceAgentId' : '412',
      }
    };

    return _httpCustom(context,'/homeCourse/queryCourse',param,success,error);
  }

  ///获取免费专业课程
  static Future<void> getHomeFeeClass(context, String key, dataCallBack success, errorCallBack error) async {
    var param = {
      'pageNum' : '1',
      'pageSize' : '100',
      'condition' : {
        'secondProductId' : key,
        'companyId': '6149',
        'serviceAgentId' : '412',
      }
    };

    return _httpCustom(context,'/homeCourse/feeClass',param,success,error);
  }

  ///获取公开专业课程
  static Future<void> getHomePublic(context, String key, dataCallBack success, errorCallBack error) async {
    var param = {
      'secondProductId' : key,
      'companyId': '6149',
      'serviceAgentId' : '412',
    };

    return _httpCustom(context,'/home/recomPublicLesso',param,success,error);
  }

  ///系统详情
  static Future<void> getHomeDetail(context,int ids, dataCallBack success, errorCallBack error) async {
    return _httpCustom(context,'/homeCourse/queryCommodityDetail',{
      'companyId': '6149',
      'serviceAgentId' : '412',
      'commodityId' : ids,
    },success,error);
  }
  ///课程表 queryCourseModuleList
  static Future<void> getQueryCourseModuleList(context,Map param, dataCallBack success, errorCallBack error) async {
    Map dict = {
      'companyId': '6149',
      'serviceAgentId' : '412',
    };
    dict.addAll(param);

    return _httpCustom(context,'/homeCourse/queryCourseModuleList', dict,success,error);

  }


  ///登录
  static Future<void> getLogin(context,String userName, String password, dataCallBack success, errorCallBack error) async {

    return _httpCustom(context,'/login',{
      // 'companyId': '6149',
      // 'serviceAgentId' : '412',
      'password' : password,
      'username' : userName + '@6149?serviceAgentId=412',
    },success,error);
  }

  void _http(String string, Map param,){

    // https://tianpuappschool.bjmantis.cn/hc-app


    // var param = {
    //   'companyId': '6149',
    //   'serviceAgentId' : '412'
    // };
    //
    // var param = {
    //   'pageNum' : '1',
    //   'pageSize' : '100',
    //   'condition' : {
    //     'secondProductId' : '13053',
    //     'companyId': '6149',
    //     'serviceAgentId' : '412',
    //   }
    // };
  }

  /// 自定义网络请求
  /// 当前界面的context
  /// url
  /// param 请求参数
  /// success 请求回调
  /// error 错误回调
  static Future<void> _httpCustom(context, String url, Map param,dataCallBack success, errorCallBack error) async {

    if(context != null){showLoadingView(context);}


    HttpRequest.setBaseUrl(_dict['base']);

    ResultData response = await HttpRequest.post(url, param);

    // Future.delayed(Duration(seconds: 2), () {
    //
    // });
    if(response.code == 200){
      if(context != null){dismissLoadingView(context, '');}

      Map userMap = json.decode(response.data);
      success(userMap);
    }else{
      if(context != null){dismissLoadingView(context, '');}

      Map userMap = json.decode(response.data);
      EasyLoading.showToast(userMap['message']);

      error(userMap);

    }

    // /home/productInfo -专业
    // /customer/getProvince
    // /homeCourse/feeClass - 系统
  }

  static void showLoadingView(context){
    // Navigator.push(context, DialogRouter(ShowLoadingViewDialog(true)));
    if(_isShow == true){
      _loadingViewDialog.pushShowLoading(context);
      _isShow = false;
    }


  }

  static void dismissLoadingView(context,error){
    // Navigator.pop(context);
    if(_isShow == false){
      _loadingViewDialog.dismissShowLoading(context,error: error);
      _isShow = true;
    }

  }
}